﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MyPlatForms.Ctrls
{
    public partial class Gheader_QrCode : UserControl
    {
        private string _mQrContent = "801000.19090910-0001.50000.TY63002";
        private string _mPicString = "yu3zx.com";
        private Font _fontsize = new Font(new FontFamily("黑体"),12);
        private float fontweight = 12;//18
        public Gheader_QrCode()
        {
            InitializeComponent();
            SetPic();
        }

        /// <summary>
        /// 设置中间显示内容
        /// </summary>
        public string QrContent
        {
            set
            {
                _mQrContent = value;
                if(_mQrContent.Length > 10)
                {
                    _mQrContent = _mQrContent.Substring(0, 10);
                }
                lblContent.Text = _mQrContent;
            }
        }

        public Font QrFontSize
        {
            set
            {
                _fontsize = value;
                lblContent.Font = _fontsize;
            }
            get { return _fontsize; }
        }

        /// <summary>
        /// 字体大小
        /// </summary>
        public float FontSize
        {
            get { return fontweight; }
            set
            {
                fontweight = value;
                lblContent.Font = new Font(new FontFamily("宋体"), fontweight);
            }
        } 

        /// <summary>
        /// 设置二维码
        /// </summary>
        public string SetPicString
        {
            set
            {
                _mPicString = value;
                SetPic();
            }
            get
            {
                return _mPicString;
            }
        }

        public void SetPic()
        {
            QrImgCtrl.Image = Gma_QRCode(_mPicString);
            
        }

        public void SetMode(PictureBoxSizeMode picMode)
        {
            QrImgCtrl.SizeMode = picMode;
        }

        private Bitmap Gma_QRCode(string enCodeString)
        {
            Gma.QrCodeNet.Encoding.QrEncoder qrEncoder = new Gma.QrCodeNet.Encoding.QrEncoder(Gma.QrCodeNet.Encoding.ErrorCorrectionLevel.L);
            Gma.QrCodeNet.Encoding.QrCode qrCode = qrEncoder.Encode(enCodeString);
            //qrEncoder.TryEncode(enCodeString, out qrCode);
            const int modelSizeInPixels = 4;

            Gma.QrCodeNet.Encoding.Windows.Render.FixedModuleSize moduleSize = new Gma.QrCodeNet.Encoding.Windows.Render.FixedModuleSize(modelSizeInPixels, Gma.QrCodeNet.Encoding.Windows.Render.QuietZoneModules.Zero);
            Gma.QrCodeNet.Encoding.Windows.Render.GraphicsRenderer render = new Gma.QrCodeNet.Encoding.Windows.Render.GraphicsRenderer(moduleSize, Brushes.Black, Brushes.White);

            Gma.QrCodeNet.Encoding.Windows.Render.DrawingSize dSize = render.SizeCalculator.GetSize(qrCode.Matrix.Width);
            Bitmap map = new Bitmap(dSize.CodeWidth, dSize.CodeWidth);
            Graphics g = Graphics.FromImage(map);
            render.Draw(g, qrCode.Matrix);

            return map;
        }

        private void QrImgCtrl_Resize(object sender, EventArgs e)
        {
            QrImgCtrl.Height = QrImgCtrl.Width;
            //lblContent.Top = this.Height - lblContent.Height - 10;// lblContent.Bottom
        }
    }
}
